using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame
{
  public class FPS_Counter : Microsoft.Xna.Framework.DrawableGameComponent
  {
    private ContentManager content;
    private SpriteFont font;

    private float elapsed;
    private float frameRate;
    private float frames;
    private enum vsync
    {
      on  = 1,
      off = 0
    }

    public FPS_Counter(Game game)
      : base(game)
    {
      content = new ContentManager(game.Services);
      
      font = content.Load<SpriteFont>(@"Arial");

      elapsed = 0.0f;
      frameRate = 0.0f;
      frames = 0.0f;

    }

    protected override void UnloadGraphicsContent(bool unloadAllContent)
    {
      if (unloadAllContent)
      {
        content.Unload();
      }
    }

    public override void Update(GameTime gameTime)
    {
      elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

      if (elapsed > 1.0f)
      {
        elapsed = 0;
        frameRate = frames;
        frames = 0;
      }
      else
      {
        frames ++;
      }
    }

    public void Draw(SpriteBatch batch, bool vSyncStatus, int xPos, int yPos)
    {
      string value = vSyncStatus == true ? "on" : "off";

      batch.DrawString(font,string.Format("{0} ({1})","FPS: " + frameRate.ToString("0.00"),"VSync: " + value), new Vector2(xPos, yPos), Color.Yellow);
    }
  }
}
